package com.xiyou.luckdraw.web.controller;

import com.xiyou.luckdraw.api.dal.entitys.UserInfo;
import com.xiyou.luckdraw.api.dto.upload.UploadRequest;
import com.xiyou.luckdraw.api.dto.upload.UploadResponse;
import com.xiyou.luckdraw.api.dto.user.*;
import com.xiyou.luckdraw.api.service.IUploadService;
import com.xiyou.luckdraw.api.service.IUserInfoService;
import com.xiyou.luckdraw.common.constants.ResultCodeConstants;
import com.xiyou.luckdraw.common.result.ResponseData;
import com.xiyou.luckdraw.common.result.ResponseUtils;
import com.xiyou.luckdraw.common.utils.ExportUtils;
import com.xiyou.luckdraw.common.utils.IPUtils;
import com.xiyou.luckdraw.common.utils.ReqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: UploadController
 * @Description: 图片上传
 * @author: limingxing
 * @Date: 2019-10-10 19:33
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Api(value = "用户信息Controller", tags = {"用户操作接口"})
public class UserInfoController {

    @Autowired
    private IUserInfoService userInfoService;


    @ApiOperation(value = "获取用户列表")
    @PostMapping("/list")
    public ResponseData<UserInfoListResponse> list(@RequestBody UserInfoListRequest request) {
        UserInfoListResponse response = userInfoService.getUserList(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }


    @ApiOperation(value = "更新用户状态")
    @PostMapping("/updateByUserId")
    public ResponseData<UpdateUserInfoResponse> updateByUserId(@RequestBody UpdateUserInfoRequest request) {
        UpdateUserInfoResponse response = userInfoService.updateByUserId(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @ApiOperation(value = "统计一周新增用户数")
    @PostMapping("/selectCountWeek")
    public ResponseData<CountUserWeekResponse> selectCountWeek() {
        CountUserWeekResponse response = userInfoService.selectCountWeek();
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }



    @ApiOperation(value = "查询活跃用户")
    @PostMapping("/getActiveUser")
    public ResponseData<UserInfoListResponse> getActiveUser(@RequestBody ActiveUserRequest request) {
//        String startTime = "2019-10-01 00:58:21";
//        String endTime = "2019-10-17 21:04:51";
        UserInfoListResponse response = userInfoService.selectActiveUser(request);
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }

    @ApiOperation(value = "统计一周活跃用户量")
    @PostMapping("/countActiveUser")
    public ResponseData<CountUserWeekResponse> countActiveUser() {
        CountUserWeekResponse response = userInfoService.countActiveUser();
        if (response.getCode().equals(ResultCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtils().setData(response);
        }
        return new ResponseUtils().setData(response.getMsg());
    }



    @ApiOperation(value = "用户信息Excel导出")
    @GetMapping("/downloadExcel")
    public ResponseData<UpdateUserInfoResponse> downloadExcel(HttpServletRequest request, HttpServletResponse response) {

        List list = userInfoService.downLoadExcel(request);
        String title = "用户信息表";
        String[] cellValues = {"ID","用户ID","兑换劵张数","答题剩余次数","答题次数","手机号码","用户昵称","状态","头像","性别"," 创建时间"};//Excel表头
        String[] titlename = {"id","userId","tntegralNum","coupon","answerCount","phone","nickName","enabled","photo","gender","createTime"};//对应数据字段
        Integer[] columnWidths = {20, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25};//Excel列宽度
        if(null != list) {
            ExportUtils.createExcel(request, response, title, columnWidths, cellValues, list, titlename);
        }
        return new ResponseUtils().setData("OK");
    }

}
